<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <!-- <el-form-item label="音乐编码" prop="audioCode">
                <el-input v-model="queryParams.audioCode" placeholder="请输入音乐编码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="音乐年代" prop="audioEra">
                <el-input v-model="queryParams.audioEra" placeholder="请输入音乐年代" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="音乐作者" prop="audioAuthor">
                <el-input v-model="queryParams.audioAuthor" placeholder="请输入音乐作者" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="音乐来源" prop="audioSource">
                <el-input v-model="queryParams.audioSource" placeholder="请输入音乐来源" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="音乐类属" prop="audioGeneric">
                <el-input v-model="queryParams.audioGeneric" placeholder="请输入音乐类属" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="音乐小类" prop="audioSmallClass">
                <el-input v-model="queryParams.audioSmallClass" placeholder="请输入音乐小类" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="音乐大小" prop="audioSize">
                <el-input v-model="queryParams.audioSize" placeholder="请输入音乐大小" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="audioState">
                <el-input v-model="queryParams.audioState" placeholder="请输入状态" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <!-- <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item> -->
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增班次</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="basicaudioList" @selection-change="handleSelectionChange">
            <!-- <el-table-column label="班次编号" align="center" prop="owner_id" /> -->
            <el-table-column label="班次名称" align="center" prop="name" />
            <el-table-column label="开始时间" align="center" prop="start" />
            <el-table-column label="结束时间" align="center" prop="finish" />
            <el-table-column label="状态" align="center" prop="running">
                <template slot-scope="scope">
                    <div>{{ scope.row.running ? '工作中' : '休息中' }}</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">

                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                        @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改音乐对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="班次名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入班次名称" />
                </el-form-item>
                <el-form-item label="开始时间" prop="start">
                    <el-time-picker v-model="form.start" value-format='HH:mm:ss' format='HH:mm:ss' placeholder="任意时间点"
                        style="width: 100%;">
                    </el-time-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="finish">
                    <el-time-picker v-model="form.finish" value-format='HH:mm:ss' format='HH:mm:ss' placeholder="任意时间点"
                        style="width: 100%;">
                    </el-time-picker>

                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>


import { getOwner, eitOwner, putOwner, delOwner } from "@/api/basicaudio";

export default {
    name: "Basicaudio",
    data() {
        return {
            // 遮罩层

            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 音乐表格数据
            basicaudioList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {

                audioCode: null,
                audioEra: null,
                audioAuthor: null,
                audioSource: null,
                audioGeneric: null,
                audioSmallClass: null,
                audioSize: null,
                audioCoverImage: null,
                audioFile: null,
                audioState: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    { required: true, message: "班次名称不能为空", trigger: "blur" },
                ],
                start: [
                    { required: true, message: "开始时间不能为空", trigger: "blur" },
                ],
                finish: [
                    { required: true, message: "结束时间不能为空", trigger: "blur" },
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询音乐列表 */
        getList() {
            this.loading = true;
            getOwner(this.queryParams).then(response => {

                this.basicaudioList = response.data.data;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                name: null,
                start: null,
                finish: null,
                owner_id: null
            };
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.audioId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增班次";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {

            this.selectedTime = row.start
            this.selectedFin = row.finish
            this.form = row
            this.open = true;
            this.title = "修改班次";

        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.owner_id != null) {
                        putOwner(this.form).then(response => {

                            this.$message({
                                message: response.data.msg,
                                type: 'success'
                            });
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        eitOwner(this.form).then(response => {

                            this.$message({
                                message: response.data,
                                type: 'success'
                            });
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            let data = {
                owner_id: row.owner_id
            }
            this.$confirm('是否确认删除名称为"' + row.name + '"的设备？').then(() => {
                delOwner(data).then((res) => {
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                    this.getList();
                    console.log(res);
                })
            })

        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('business/basicaudio/export', {
                ...this.queryParams
            }, `basicaudio_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>